import { defineStore } from 'pinia'
import type { UserStoreType } from '@/stores/interface'
import { delAccessToken,getAccessToken } from '@/utils/'
import { getUser } from '@/api'

export const useUserStore = defineStore('user', {
  persist: true,
  state: (): { uid: number; gender: string; nickname: string; state: boolean; email: string; username: string } => {
    return {
      uid: 0,
      username: '',
      nickname: '',
      gender: '',
      email: '',
      state:false
    }
  },
  getters: {},
  actions: {
    /**
     * 退出登录
     */
    logout() {
      this.$reset()
      delAccessToken()
      location.reload()
    },
    loadUserStore() {
      setTimeout(() => {
        getUser({
          username:this.username
        }).then(({ code, msg, data }: any) => {
          if (code == 2000 && data != null) {
            this.uid = data.id
            this.username = data.username
            this.nickname = data.nickname
            this.gender = data.gender
            this.email = data.email
          }
        })
      }, 1000)
    },
    setState(state: boolean){
      this.state = state
    },
    checkLogin(): boolean{
      return getAccessToken() != '' && getAccessToken() != null;
    }
  }
})
